/*
 * @Description:  任务
 * @Author: Myruun
 * @Date: 2021-04-19 16:03:57
 * @LastEditors: Myruun
 * @LastEditTime: 2021-08-27 11:31:40
 * @FilePath: \serve\controller\routers.js
 */
const mysqlQuery = require("../unit/mysql.js");
const units = require("../unit/index.js");
// 获取任务列表
exports.Index = async (ctx, next) => {
  let body = ctx.request.body;
  const id = body.id || "";
  let sql = "";
  if (id) {
    sql =
      "SELECT * FROM router  WHERE state = '1' and id ='" +
      id +
      "' order by id desc";
  } else {
    sql = "SELECT * FROM router  WHERE state = '1'";
  }
  let obj = await mysqlQuery(sql);

  const tree = units.getTree(obj.data, 'id', 'uid', 'children')
  ctx.response.body = { code: 200, data: tree };
};

// 添加任务
exports.add = async (ctx, next) => {
  let body = ctx.request.body;
  const uid = body.uid || 0;
  const name = body.name || "";
  const path = body.path || "";
  const type = body.type || 0;
  const state = body.state || 0;
  const auth = body.auth || "";
  const icon = body.icon || "";
  const meta = body.meta || "";

  const time = Math.round(new Date() / 1000);
  let sql = `INSERT INTO router (id, uid, title, path, type, state, auth, icon, meta, update_time) VALUES (0,'${uid}','${name}','${path}','${type}','${state}','${auth}','${icon}','${meta}','${time}')`;
  let obj = await mysqlQuery(sql);
  ctx.response.body = { code: 200, data: { msg: "添加成功" } };
};

